import {magic} from '@lexmis/magic-ui/pc';
export function thousandsFormat(num){
	if(!num && num !== 0) return '';
	const numArr = num.toString().split('.');
	numArr[0] = numArr[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
	return numArr.join('.');
}
export function getScriptFunc(str){
	let func = null;
	try{
		func = eval(str);
		return func;
	}catch(error){
		console.log(error);
		return false;
	}
}
export function toDate(v, format){
	format = format || 'yyyy-MM-dd HH:mm';
	if(!v) return '';
	let d = v;
	if(typeof v === 'string'){
		if(v.indexOf('/Date(') > -1){
			d = new Date(parseInt(v.replace('/Date(', '').replace(')/', ''), 10));
		}else d = new Date(Date.parse(v.replace(/-/g, '/').replace('T', ' ').split('.')[0]));
	}else{
		d = new Date(v);
	}
	const o = {
		'M+':d.getMonth() + 1,
		'd+':d.getDate(),
		'h+':d.getHours(),
		'H+':d.getHours(),
		'm+':d.getMinutes(),
		's+':d.getSeconds(),
		'q+':Math.floor((d.getMonth() + 3) / 3),
		S:d.getMilliseconds(),
	};
	if(/(y+)/.test(format)){
		format = format.replace(RegExp.$1, (d.getFullYear() + '').substr(4 - RegExp.$1.length));
	}
	for(const k in o){
		if(new RegExp('(' + k + ')').test(format)){
			format = format.replace(
				RegExp.$1,
				RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length),
			);
		}
	}
	return format;
}
export function cloneDeep(obj, hash = new WeakMap()){
	// 如果是原始值，直接返回
	if(obj === null || typeof obj !== 'object') return obj;
	// 处理循环引用
	if(hash.has(obj)) return hash.get(obj);
	const result = Array.isArray(obj) ? [] : {};
    // 防止循环引用
	hash.set(obj, result);

	for(const key in obj){
		if(Object.prototype.hasOwnProperty.call(obj, key)){
			// 递归深拷贝
			result[key] = cloneDeep(obj[key], hash);
		}
	}
	// 处理特殊类型：Date, RegExp等
	if(obj instanceof Date) return new Date(obj);
	if(obj instanceof RegExp) return new RegExp(obj);

	return result;
}
export function t(key){
	return magic.core.translate(key);
}